package flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author：LJ
 * @Description：享元工厂
 * @Date: 2017/12/3
 * @Modified By:
 */
public class FlyweightFactory {

    //定义一个池容器
    private static Map<String, Flyweight> pool = new HashMap<String, Flyweight>();

    //享元工厂
    public static Flyweight getFlyweight(String extrinsic) {

        //需要返回的对象
        Flyweight flyweight = null;
        //在池中没有该对象
        if (pool.containsKey(extrinsic)) {
            flyweight = pool.get(extrinsic);
        } else {
            //根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight1(extrinsic);//或 new ConcreteFlyweight2(extrinsic);
            //放入池中
            pool.put(extrinsic, flyweight);
        }
        return flyweight;
    }
}
